<?php
namespace DAOZHEN\D;
class Guests extends \ZHMVC\D\DataBase
{
    public function getAllNum($parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            
            if($s=="")
            {
                $sql="select count(*) as num from dz_guests";
                $data=$this->_db->getOne($sql);
            }
            else
            {
                //去掉第一个逻辑符号
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
            
                $sql="select count(*) as num from dz_guests where ".$tempS;
                $data=$this->_db->getOne($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select count(*) as num from dz_guests where ".$parameter;
            $data=$this->_db->getOne($sql);
        }
        else
        {
            $sql="select count(*) as num from dz_guests";
            $data=$this->_db->getOne($sql);
        }
        if(empty($data)==true)
        {
            $data["num"]=0;
        }
        $this->_rows=$this->_db->getRowCount();
        return $data;
    }
            
    public function getAll($parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            
            if($s=="")
            {
                $sql="select * from dz_guests";
                $datas=$this->_db->getAll($sql);
            }
            else
            {
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                $sql="select * from dz_guests where ".$tempS;
                $datas=$this->_db->getAll($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select * from dz_guests where ".$parameter."";
            $datas=$this->_db->getAll($sql);
        }
        else
        {
            $sql="select * from dz_guests";
            $datas=$this->_db->getAll($sql);
        }
   
        $this->_rows=$this->_db->getRowCount();
        return $datas;
    }
            
    public function getRows()
    {
        return $this->_rows;
    }
            
    public function getPages($limit,$parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            if($s=="")
            {
                $sql="select * from dz_guests order by id desc LIMIT ".$limit."";
                $datas=$this->_db->getAll($sql);
            }
            else
            {
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                $sql="select * from dz_guests where ".$tempS." order by id desc LIMIT ".$limit."";
                $datas=$this->_db->getAll($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select * from dz_guests where ".$parameter." order by id desc LIMIT ".$limit."";
            $datas=$this->_db->getAll($sql);
        }
        else
        {
            $sql="select * from dz_guests order by id desc LIMIT ".$limit."";
            $datas=$this->_db->getAll($sql);
        }
        $this->_rows=$this->_db->getRowCount();
        return $datas;
    }
            
    public function getOne($postid)
    {
        $sql="select * from dz_guests where id=:id";
        $bind=array(":id"=>$postid);
        $data=$this->_db->getOne($sql,$bind);
        $this->_rows=$this->_db->getRowCount();
        return $data;
    }
            
    public function getOne1($parameter="")
    {
        if(is_array($parameter)==true)
        {
            $bind=array();
            $s="";
            $tempS=parent::parseSql($parameter);
            $bind=$tempS["bind"];
            $s=$tempS["s"];
            
            if($s=="")
            {
                $sql="select * from dz_guests";
                $datas=$this->_db->getOne($sql);
            }
            else
            {
                $s_a=explode(" ", $s);
                $tempS="";
                for($j=1;$j<count($s_a);$j++)
                {
                    $tempS.=$s_a[$j]." ";
                }
                $sql="select * from dz_guests where ".$tempS;
                $datas=$this->_db->getOne($sql,$bind);
            }
        }
        elseif($parameter!="")
        {
            $sql="select * from dz_guests where ".$parameter."";
            $datas=$this->_db->getOne($sql);
        }
        else
        {
            $sql="select * from dz_guests";
            $datas=$this->_db->getOne($sql);
        }
   
        $this->_rows=$this->_db->getRowCount();
        return $datas;
    }
            
    public function add($guest_id,$name,$gender,$age,$phone,$is_vip,$register_time,$status)
    {
        $IdWorker = \wantp\Snowflake\IdWorker::getIns();
        $id = $IdWorker->id();
        $sql="insert into dz_guests (`guest_id`,`name`,`gender`,`age`,`phone`,`is_vip`,`register_time`,`status`) values (:guest_id,:name,:gender,:age,:phone,:is_vip,:register_time,:status)";
        $bind=array(":guest_id"=>$guest_id,":name"=>$name,":gender"=>$gender,":age"=>$age,":phone"=>$phone,":is_vip"=>$is_vip,":register_time"=>$register_time,":status"=>$status);
        $this->_db->update($sql,$bind);
        $this->_lastid=$id;
        return $this->_lastid;
    }
            
    public function update($guest_id,$name,$gender,$age,$phone,$is_vip,$register_time,$status)
    {
        $sql="update dz_guests set `guest_id`=:guest_id,`name`=:name,`gender`=:gender,`age`=:age,`phone`=:phone,`is_vip`=:is_vip,`register_time`=:register_time,`status`=:status where id=:id";
        $bind=array(":guest_id"=>$guest_id,":name"=>$name,":gender"=>$gender,":age"=>$age,":phone"=>$phone,":is_vip"=>$is_vip,":register_time"=>$register_time,":status"=>$status,":id"=>$id);
        $this->_db->update($sql,$bind);
        return 1;
    }
            
    public function delete($id)
    {
        $sql="delete from dz_guests where id=:id";
        $bind=array(":id"=>$id);
        $this->_db->update($sql,$bind);
        return 1;
    }

    public function addOne()
    {
        $IdWorker = \wantp\Snowflake\IdWorker::getIns();
        $id = $IdWorker->id();
        $sql = "insert into dz_guests(`id`) values (:id)";
        //echo $sql;
        $bind = array(":id" => $id);
        $this->_db->update($sql, $bind);
        $this->_lastid = $id;
        return 1;
    }
    
    public function getLastId(){
        return $this->_lastid;
    }
}